// index page
// ============

define(['require', 'zepto', 'wx', 'sui', 'submitForm'],
     function() {
          return {
               init: function() {
                    this.initSubmitForm();
                    this.initPicker();
                    this.initSelectCity();
               },
               
               initSelectCity: function() {
                    $(document).on("click", ".drop-off-popup .item-inner .item-title", function(e) {
                         e.preventDefault();
                         var currentName = $(this).data("name")
                         var currentId = $(this).data("id")
                         $(".drop-off-val").val(currentName)
                         $(".drop-off-id").val(currentId)
                         $.closeModal(".drop-off-popup");
                    })
               },

               initPicker: function() {
                    /* $("#alias").picker({
                         toolbarTemplate: '<header class="bar bar-nav">\
                          <button class="button button-link pull-right close-picker">确定</button>\
                          <h1 class="title">称谓</h1>\
                          </header>',
                         cols: [{
                              textAlign: 'center',
                              values: ['女士', '先生']
                         }]

                    });*/
                    $("#agentType").picker({
                         toolbarTemplate: '<header class="bar bar-nav">\
                          <button class="button button-link pull-right close-picker">确定</button>\
                          <h1 class="title">类型</h1>\
                          </header>',
                         cols: [{
                              textAlign: 'center',
                              values: ['PERSONAL', 'COMPANY'],
                              displayValues: ['个人', '公司']
                         }],
                         formatValue: function(picker, value, displayValue) {
                              $(this.input).next().val(value);
                              return displayValue;
                         }
                    })
               },

               initSubmitForm: function() {
                    var $form = $("form");
                    $("#submit").on("click", function() {
                         var type = $("input[name='type']");
                         var districtId = $("input[name='districtId']");
                         var name = $("input[name='name']");
                         var mobile = $("input[name='mobile']");
                         var email = $("input[name='email']");
                         var companyName = $("input[name='companyName']");
                         var primaryAgentId = $("input[name='primaryAgentId']");
                         var rate = $("input[name='rate']");


                         if(!type.val()) {
                              $.toast("请选择类型");
                              return false;
                         }
                         
                         if(type.val() === 'COMPANY') {
                        	 if(!companyName.val()) {
                                 $.toast("请输入公司名称");
                                 return false;
                            }
                         }
                         
                         if(!districtId.val()) {
                              $.toast("请选择关联分区");
                              return false;
                         }
                         if(!name.val()) {
                              $.toast("请输入姓名");
                              return false;
                         }
                         if(!mobile.val()) {
                              $.toast("请输入手机号");
                              return false;
                         }
                         if(!/^((13)|(15)|(17)|(18))\d{9}$/.test(mobile.val())) {
                              $.toast("请输入正确的手机号");
                              return false;
                         }
                         if(!email.val()) {
                              $.toast("请输入邮箱");
                              return false;
                         }
                         if(!/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(email.val())) {
                              $.toast("请输入正确的邮箱");
                              return false;
                         }

                         var url = window.__CTX__.root + "/agent-applies";
                         $.showPreloader('正在提交信息...')
                         $.ajax({
                              url: url,
                              type: 'POST',
                              data: {
                                   "companyName": companyName.val(),
                                   "type": type.val(),
                                   "districtId": districtId.val(),
                                   "name": name.val(),
                                   "mobile": mobile.val(),
                                   "email": email.val(),
                                   "rate": rate.val(),
                                   "primaryAgentId": primaryAgentId.val()
                              },
                              dataType: "json",
                              success: function(data) {
                                   $.hidePreloader();
                                   data = typeof data === 'String' ? JSON.parse(data) : data;
                                   if(parseInt(data.code) == 0) {
                                         $.toast("注册成功，请等待我们的联络");
                                         setTimeout(function() {
                                              window.location.href = window.__CTX__.root + "/login"
                                         }, 500)
                                   } else {
                                        $.toast(data.message);
                                   }
                              },
                              error: function() {
                                   $.toast("创建失败");
                              }
                         })
                         /* $form.submit();*/
                    });
               }
          }
     })